package com.example.demo.config;

import com.example.demo.common.AppVariable;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/*
* 用户拦截器
* */
public class LoginIntercept implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(AppVariable.SESSION_USERINFO_KEY)!=null){
            //用户已登录
            return true;
        }
        //没有登录直接跳转到登录页面
        response.sendRedirect("/login.html");
        //执行拦截器之前的执行的方法，如果返回false，后续的拦截方法不会执行。
        return false;
    }
}
